import { Message } from 'element-ui'
import axios from 'axios'
import qs from 'qs'

axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

const apiBase = process.env.VUE_APP_API_BASE;
const service = axios.create({
    timeout: 30000,
    paramsSerializer: params => {
        return qs.stringify(params, { arrayFormat: 'repeat' });
    }
})

service.interceptors.request.use(
    config => {
        if (!config.external) {
            config.url = apiBase + config.url;
        }
        if (!config.headers) {
            config.headers = {}
        }
        if (config.useFormData) {
            config.headers["Content-Type"] = 'application/x-www-form-urlencoded';
            config.transformRequest = [data => {
                let result = "";
                Object.keys(data).forEach(v => {
                    result += v + "=" + data[v] + "&";
                })
                return result.length > 0 ? result.substring(0, result.length - 1) : result;
            }]
        }
        config.headers["ark"] = "api";
        //判断如果是post、delete方式则提交参数为data
        // if(config.method == 'get' || config.method == 'GET'){
        //     if(!config.params){config.params={}}
        //     config.params.timestamp = new Date().getTime();
        // }
        if(config.method == "DELETE" || config.method == "delete"){
            config.data = config.params;
        }
        return config;
    },
    error => {
        console.error(error);
        return Promise.reject(error);
    }
)

service.interceptors.response.use(
    response => {
        if (response.status == 200) {
            // 返回消息体
            let data = response.data;
            return data === null ? true : data;
        }
    },
    error => {
        console.log(error);
        if (error.response.status === 401) {
            setTimeout(() => {
                window.location = '/';
            }, 500);
        } else if (error.response.status === 516) {
            // 自定义错误处理
            let onerror = error.response.config.onerror;
            if (onerror && typeof(onerror) == 'function') {
                onerror(error);
            } else {
                // Message({
                //     message: (error.response.data),
                //     type: 'error'
                // });
            }
        } else if (error.response.status === 517) {
            // 数据校验处理
            let onerror = error.response.config.onerror;
            if (onerror && typeof(onerror) == 'function') {
                onerror(error);
            } else {
                let msg=''
                for(let i=0;i< error.response.data.length;i++){
                    msg+=error.response.data[i].v_tip+'，'
                }
                msg=msg.length>0?msg.substring(0,msg.length-1):msg
                // Message({
                //             message: msg,
                //             type: 'error'
                //         })
            }
        }else {  }
        return Promise.reject(error);
    }
)

export default service